﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Base.BLL;
using MPSystem.Controllers;
using MPSystem.Models;

namespace MPSystem.Views.YFQ
{
    /// <summary>
    /// WYDataQuery_Condition.xaml 的交互逻辑  围岩移动数据查询页面
    /// </summary>
    public partial class WYDataQuery_Condition : Page
    {
        public Dictionary<string, string> Cond = new Dictionary<string, string>();
        public static WYDataQuery_Condition _Me = null;
        public static WYDataQuery_Condition Me
        {
            get
            {
                if (_Me == null)
                {
                    _Me = new WYDataQuery_Condition();
                    _Me.UpdateCond();
                }
                return _Me;
            }
            set { _Me = value; }
        }

        int xdbh = -1,lx=-1;
        public WYDataQuery_Condition()
        {
            InitializeComponent();
            LoadData();
            Me = this;
        }
        public void LoadData()
        {
            #region 加载数据类型
            string strLX = "0&顶板离层,1&顶板下沉";
            string[] arLX = strLX.Split(',');
            DataTable tempTable = new DataTable();
            DataColumn bh1 = new DataColumn("bh", Type.GetType("System.Int32"));
            DataColumn mc2 = new DataColumn("mc", Type.GetType("System.String"));
            tempTable.Columns.Add(bh1);
            tempTable.Columns.Add(mc2);
            foreach (var r in arLX)
            {
                string[] arrl = r.ToString().Split('&');
                DataRow dr0 = tempTable.NewRow();
                dr0["bh"] = int.Parse(arrl[0]);
                dr0["mc"] = arrl[1].ToString();
                tempTable.Rows.Add(dr0);
            }
            tempTable.AcceptChanges();
            CB_LX.ItemsSource = tempTable.DefaultView;
            tempTable.Dispose();
            #endregion

            #region 加载加权
            string strJQ = "0&所有原始数据,2&2H,4&4H,8&8H,12&12H,24&24H";
            string[] arJQ = strJQ.Split(',');
            tempTable = new DataTable();
            DataColumn dc1 = new DataColumn("bh", Type.GetType("System.Int32"));
            DataColumn dc2 = new DataColumn("mc", Type.GetType("System.String"));
            tempTable.Columns.Add(dc1);
            tempTable.Columns.Add(dc2);
            foreach (var r in arJQ)
            {
                string[] arrl = r.ToString().Split('&');
                DataRow dr0 = tempTable.NewRow();
                dr0["bh"] = int.Parse(arrl[0]);
                dr0["mc"] = arrl[1].ToString();
                tempTable.Rows.Add(dr0);
            }
            tempTable.AcceptChanges();
            CB_Qfs.ItemsSource = tempTable.DefaultView;
            tempTable.Dispose();
            #endregion

            #region 加载A值，B值选择
            string strP = "未设,>,>=,=,<,<=";
            string[] arP = strP.Split(',');
            foreach (var ar in arP)
            {
                CBA.Items.Add(ar);
                CBB.Items.Add(ar);
            }
            #endregion

            #region 加载是否报警
            string strSfbj = "-1&全部,1&是,0&否";
            string[] arSfbj = strSfbj.Split(',');
            DataTable tempTable1 = new DataTable();
            DataColumn dcbj1 = new DataColumn("bh", Type.GetType("System.Int32"));
            DataColumn dcbj2 = new DataColumn("mc", Type.GetType("System.String"));
            tempTable1.Columns.Add(dcbj1);
            tempTable1.Columns.Add(dcbj2);
            foreach (var r in arSfbj)
            {
                string[] arrl = r.ToString().Split('&');
                DataRow dr0 = tempTable1.NewRow();
                dr0["bh"] = int.Parse(arrl[0]);
                dr0["mc"] = arrl[1].ToString();
                tempTable1.Rows.Add(dr0);
            }
            tempTable1.AcceptChanges();
            CB_Sfbj.ItemsSource = tempTable1.DefaultView;
            tempTable1.Dispose();
            #endregion

            #region 读取传感器
            LoadBH();
            BegTime.SelectedDate = DateTime.Now;
            EndTime.SelectedDate = DateTime.Now;
            CB_LX.SelectedIndex = 0;
            CB_Qfs.SelectedIndex = 0;
            CBA.SelectedIndex = 0;
            TPA.Text = "0";
            CBB.SelectedIndex = 0;
            TPB.Text = "0";
            //默认加权值为原始数据
            CB_LX.SelectedIndex = 0;
            CB_Sfbj.SelectedIndex = 0;
            #endregion

        }
        #region 读取传感器编号
        public void LoadBH()
        {
            if (xdbh == DataQuery_WYController.xdbh && lx==CB_LX.SelectedValue.ToInt()) return;
            xdbh = DataQuery_WYController.xdbh;
            var li_ylfz = ConfigService.GetDblcWzEnabled(xdbh, CB_LX.SelectedIndex== 0);
            Items = new Dictionary<string, object>();
            if (li_ylfz.Count() > 0)
            {
                foreach (var r in li_ylfz)
                {
                    Items.Add(r.CGQBH.ToString(), r.CGQBH.ToString());
                }
            }
            CGQ.ItemsSource = Items;
            CGQ.SelectedItems = Items.Clone() as Dictionary<string, object>;
        }
        #endregion
        public void UpdateCond()
        {
            Cond.Clear();
            try
            {
                string Query_P = "";
                if (CBA.SelectedIndex > 0)
                    if (TPA.Text != "")
                        Query_P = " LCDATAA" + CBA.SelectedValue.ToString() + TPA.Text;
                if (CBB.SelectedIndex > 0)
                    if (TPB.Text != "")
                        if (Query_P == "")
                            Query_P = " LCDATAB" + CBB.SelectedValue.ToString() + TPB.Text;
                        else
                            Query_P += " AND LCDATAB" + CBB.SelectedValue.ToString() + TPB.Text;
                //CB_LX
                if (CB_LX.SelectedIndex > 0)
                    if (TPB.Text != "")
                        if (Query_P == "")
                            Query_P = " LCDATAB" + CBB.SelectedValue.ToString() + TPB.Text;
                        else
                            Query_P += " AND LCDATAB" + CBB.SelectedValue.ToString() + TPB.Text;
                string str_List = "";
                foreach (var item in CGQ.SelectedItems.Keys)
                {
                    if (str_List == "")
                        str_List = item.ToString();
                    else
                        str_List += "," + item.ToString();
                }
                Cond.Add("lx", CB_LX.SelectedValue.ToString());
                Cond.Add("bhs", str_List);
                Cond.Add("dtStart", BegTime.Text);
                Cond.Add("dtEnd", EndTime.Text);
                Cond.Add("jq", CB_Qfs.SelectedValue.ToString());
                Cond.Add("where", Query_P);
                Cond.Add("sfbj", CB_Sfbj.SelectedValue.ToString());
            }
            catch(Exception ex) { }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {            
            this.CloseParentWindow();
        }
        #region 多选下拉框使用
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private Dictionary<string, object> _items;
        private Dictionary<string, object> _selectedItems;
        public Dictionary<string, object> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
        public Dictionary<string, object> SelectedItems
        {
            get
            {
                return _selectedItems;
            }
            set
            {
                _selectedItems = value;
                NotifyPropertyChanged("SelectedItems");
            }
        }
        private void Submit()
        {
            foreach (KeyValuePair<string, object> s in SelectedItems)
                MessageBox.Show(s.Key);
        }
        #endregion

        private void CB_LX_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var li_ylfz = ConfigService.GetDblcWzEnabled(xdbh, CB_LX.SelectedIndex == 0);
            Items = new Dictionary<string, object>();
            if (li_ylfz.Count() > 0)
            {
                foreach (var r in li_ylfz)
                {
                    Items.Add(r.CGQBH.ToString(), r.CGQBH.ToString());
                }
            }
            CGQ.ItemsSource = Items;
            CGQ.SelectedItems = Items.Clone() as Dictionary<string, object>;
        }
    }
}
